define(['./controllers','ducalendar','swiper','moment'], function (controllers,ducalendar,swiper,moment) {
    'use strict';
    controllers.controller('detailCtrl', ['$scope', '$timeout', '$state','lineServices','$stateParams','$compile',
        function ($scope,$timeout, $state,lineServices,$stateParams,$compile) {

            $scope.lineid=$stateParams.lineid;
            $scope.line;
            $scope.line_calendar;
            var calendar ={};
            var activeDate;
            lineServices.getLine($scope.lineid).then(function (lineData) {
                console.log(lineData);
                $scope.line = lineData;
            });
            lineServices.getCalendar($scope.lineid).then(function (lineCalendar) {
                $scope.line_calendar = lineCalendar;
                angular.forEach($scope.line_calendar, function (data, index, array) {
                    var sdate = moment(data.sdate).format("YYYY-MM");
                    if(calendar[sdate]==undefined){
                        calendar[sdate]=[];
                    }
                    calendar[sdate].push(data);
                    if(sdate==moment().format("YYYY-MM")){
                        activeDate = sdate;
                    }else if(moment(sdate).isBefore(activeDate)){
                        activeDate = sdate;
                    }
                });
                for(var caTemp in calendar){
                    $('#data-tab').append('<a class="btn btn-product " id="d'+caTemp+'" ng-click="getcurDate(\''+moment(caTemp).format("YYYY")+'\',\''+moment(caTemp).format("MM")+'\')">'+moment(caTemp).format("YYYY")+'年'+moment(caTemp).format("MM")+'月</a>');
                }
                ////第一次时把日历调出
                $timeout(function () {
                    $scope.getcurDate(moment(activeDate).format("YYYY"),moment(activeDate).format("MM"));

                },1000);
                //$scope.getcurDate(moment(activeDate).format("YYYY"),moment(activeDate).format("MM"));
                $('#d' +activeDate).addClass('active');
                $scope.$evalAsync(function (){
                    $compile($("#data-tab"))($scope);
                })
            });
            //首页动画
            $scope.pageClass = 'page-home';

            var swiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination'
                //autoplay : 3000
            });

            $scope.getcurDate = function (y, m) {
                $('#data-tab a').removeClass('active');
                $('#d' + y + "-" + m).addClass('active');
                $timeout(function () {
                    $scope.$apply(function () {
                        ducalendar.showCalendar('#calendar', y, m, function () {
                            var caList = calendar[y + "-" + m];
                            for (var i = 0; i < caList.length; i++) {
                                var date = moment(caList[i].sdate).format("YYYY-MM-D");
                                $('[my-date="' + date + '"]').append('<div class="day-info">¥' + caList[i].full_payment + '</div><div class="day-info">剩余' + caList[i].inventory + '</div>');
                            }
                        });
                    });
                });
            }


            // 选项卡
            $('#table').find('a').on('click', function (e) {
                e.preventDefault();
                var _this = $(this);
                var index = _this.index();
                var $tabs = $('.tabs-box');
                _this.addClass('activity').siblings().removeClass('activity');
                $tabs.hide();
                $tabs.eq(index).show();
            });
        }
    ])

})
